package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.domain.Settings;
import com.tanhua.dubbo.mappers.SettingsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 通知设置服务提供者
 */
@DubboService
public class SettingsApiImpl extends ServiceImpl<SettingsMapper, Settings> implements SettingsApi {
    @Autowired
    private SettingsMapper settingsMapper;

    @Override
    public void saveNotificationsSetting(Settings settings) {
        //判断当前用户是否保存过通知设置
        LambdaQueryWrapper<Settings> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Settings::getUserId,settings.getUserId());
        //原有的setting对象
        Settings oldSettings = settingsMapper.selectOne(wrapper);

        if(oldSettings == null){
            //通知设置表无该对象,保存该对象
            settingsMapper.insert(settings);
        }else{
            LambdaUpdateWrapper<Settings> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Settings::getUserId,settings.getUserId())
                    .set(Settings::getGonggaoNotification,settings.getGonggaoNotification())
                    .set(Settings::getPinglunNotification,settings.getPinglunNotification())
                    .set(Settings::getLikeNotification,settings.getLikeNotification());
            settingsMapper.update(settings,updateWrapper);
        }
    }

    @Override
    public Settings getByUserId(Long userId) {
        LambdaQueryWrapper<Settings> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Settings::getUserId,userId);
        return settingsMapper.selectOne(wrapper);
    }
}
